<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script src="./js/jquery-1.7.2.min.js"></script>
</head>
<style>
.fontClass{
	font-size : 25px;
	font-weight : blod;
}

div.line{
height:5px;
border-top:2px solid black;
width:100%;
}

.testItem{
	margin:auto;
	height:90px;
}

</style>
<script>
window. addEventListener ('message', function(event) { 
	//event.data获取传过来的数据
	if(event.data.type == "print"){
		printTest();
	}else if(event.data.type == "test"){
		generate(event.data.testData);
	}
});

function printTest(){
  window.print(); 
}

//判断计算题目是否重复
//只是字符串比较 1+2 和2+1 为false
var maxCacheCount = 30; //多少个以内不重复
var resultCache = [];
function isExist(testItem){
	var result = false;
	for(var i = 0;i<resultCache.length;i++){
		if(resultCache[i].str == testItem.str){
			console.log(testItem);
			result = true;
			break;
		}
	}
	
	if(!result){
		resultCache.push(testItem);
		if(resultCache.length>maxCacheCount){
			resultCache.shift();
		}
	}
	
	return result;
}

//生成计算题目
function generate(options){
	$("#testList").empty();
	resultCache = [];
	
	//console.log(options);
	
	if(options.totalWeight >0){
		var result = [];
		var sumCount = options.total;
		
		var generatCount = 0;
		
		var itemNumberArray=[];
		jQuery.each(options.item,function(i,n){
			itemNumberArray.push(0);
		});
		
		
		while(sumCount >0 ){
			//在总的权重下面,得到一个随机数
			//通过随机数,得到当前需要整哪个计算
			
			var itemWeightArray = [];
			var calTime = 0;
			for(var i = 0 ; i< options.item.length;i++){
				calTime = Math.max(options.item[i].value,calTime);	
			}
			for(var i = 0 ; i<calTime;i++){
				for(var j = 0 ; j< options.item.length;j++){
					if(options.item[j].value>(i)){
						itemWeightArray.push(j);
					}
				}
			}
			
			//console.log(itemWeightArray);
			
			jQuery.each(itemWeightArray,function(i,n){
				var targetScript = options.item[n].method+"();";
				//console.log(targetScript);
				var testItem = eval(targetScript);
				while(!testItem  || (isExist(testItem))){
					testItem = eval(targetScript);
					generatCount++;
				}
				if(!testItem){
					console.log(targetScript);
				}
				
				//判断是否重复
				//增加生成次数超过题目总数的十倍,说明选择的项目中,题目一定有重复,跳过isExist的判断,直接生成,否则会死循环
				//if( !isExist(testItem) ){
					result.push(testItem);
					sumCount --;	
					itemNumberArray[n] = itemNumberArray[n]+1;
				//}
				
			});
		}
		
		//console.log(itemNumberArray);
		
		//开始渲染题目表格
		var table = $("<table></table>").addClass("testTable").attr("width","100%");
		var colCount = 0;
		var lastTr;
		$.each(result,function(i,n){
			colCount = i % options.colNum;
			if(colCount == 0){
				lastTr = $("<tr></tr>").addClass("testTableRow");
				table.append(lastTr);
			}
			
			var col = showTestItem(n);
			var td = $("<td></td>");
			td.append(col);
			lastTr.append(td);
		});
		
		$("#testList").append(table);
	}
}

/**
*	生成单个的计算题目
*/
function showTestItem(testItem){
	
	/*
	var testTable = $("<table></table>").addClass("shuTestItem").attr("width","100px");
	var tr = $("<tr></tr>");
	tr.append($("<td></td>"));
	tr.append($("<td></td>").attr("align","right").addClass("fontClass").text(testItem.left));
	testTable.append(tr);
	var tr2 = $("<tr></tr>");
	tr2.append($("<td></td>").attr("align" , "left").addClass("fontClass").text(testItem.flag));
	tr2.append($("<td></td>").attr("align" , "right").addClass("fontClass").text(testItem.right));
	testTable.append(tr2);
	var tr3 = $("<tr></tr>");
	tr3.append($("<td></td>").attr("colspan" , 2).append($("<div></div>").addClass("line")));
	testTable.append(tr3);
	
	var tr4 = $("<tr></tr>").attr("height","50px");
	testTable.append(tr4);
	return testTable;
	*/
	return $("<div></div>").addClass("fontClass").addClass("testItem").text(testItem.str);
}


//--------------------------------------计算方法
//20以内加减法
function innerTwenty(){
	var flag = Math.round(Math.random() * 1000);
	var one , two , flagStr;
	if(flag % 2 == 0){
		//加法
		flagStr = "+";
		two = 1+Math.round(Math.random() * 8);
		one = 1+Math.round(Math.random() * 8);
		
	}else{
		flagStr = "-";
		var number1 = 1+Math.round(Math.random() * 8);
		var number2 = 10+Math.round(Math.random() * 9);
		one = number1>number2?number1:number2;
		two = number1<number2?number1:number2;
	}
	return {
	left:one,flag:flagStr,right:two,str:one + " "+flagStr+" " + two +" = "
	}
}

//5以内加法
function innerFivePlus(){
	var one =  Math.round(Math.random() * 5);
	var two =  Math.round(Math.random() * 5);
	return {
	left:one,flag:"+",right:two,str:one + " + " + two +" = "
	}
}

//5以内减法
function innerFiveMinus(){
	var one = 1 + Math.round(Math.random() * 4);
	var two = 1+Math.round(Math.random() * (one-1));
	return {
	left:one,flag:"-",right:two,str:one + " - " + two +" = "
	}
}

//无进位加法
function innerTenPlus(){
	var one = 1 + Math.round(Math.random() * 8);
	var two = 1 + Math.round(Math.random() * (9-one));
	return {
	left:one,flag:"+",right:two,str:one + " + " + two +" = "
	}
}

//结果为10的加法
function plusTen(){
	var one = 1 + Math.round(Math.random() * 9);
	var two = 10-one;
	return {
	left:one,flag:"+",right:two,str:one + " + " + two +" = "
	}
}

//有进位的加法
function overTenPlus(){
	var one,two , result;
	result = 10+Math.round(Math.random() * 8);
	var geNum = result % 10;
	one = geNum+Math.round(1+Math.random() * (8-geNum));
	two = result - one;
	return {
	left:one,flag:"+",right:two,str:one + " + " + two +" = "
	}
}

//大数进位的加法
function bigOverPlus(){
	var one,two , result;
	result = 10+Math.round(Math.random() * 8);
	var geNum = result % 10;
	one = geNum+Math.round(1+Math.random() * (8-geNum));
	result = result + (10*(1+Math.round(Math.random() * 7)));
	two = result - one;
	return {
	left:one,flag:"+",right:two,str:one + " + " + two +" = "
	}
}

//10的减法
function minusTen(){
	var one = 10;
	var two = 1+Math.round(Math.random() * (one-1));
	return {
	left:one,flag:"-",right:two,str:one + " - " + two +" = "
	}
}

//无退位减法
function innerTenMinus(){
	var one = 5 + Math.round(Math.random() * 4);
	var two = 1+Math.round(Math.random() * (one-1));
	
	return {
	left:one,flag:"-",right:two,str:one + " - " + two +" = "
	}
}

//有退位的减法
function lessTenMinus(){
	var one,two ;
	one = 10+Math.round(Math.random() * 8);
	var geNum = one % 10;
	two = geNum+Math.round(1+Math.random() * (8-geNum));
	//one = 10 + Math.round(Math.random() * (two-1));
	return {
	left:one,flag:"-",right:two,str:one + " - " + two +" = "
	}
}

//大数退位的减法
function bigLessTenMinus(){
	var one,two ;
	one = 10*(1+Math.round(Math.random() * 7))+Math.round(Math.random() * 8);
	var geNum = one % 10;
	two = geNum+Math.round(1+Math.random() * (8-geNum));
	
	return {
	left:one,flag:"-",right:two,str:one + " - " + two +" = "
	}
}

//3个数连加
function threePlus(){
	var one = 1+Math.round(Math.random() * 8);
	var two = 1+Math.round(Math.random() * 8);
	var three = 1+Math.round(Math.random() * 8);
	
	return {
	str:one + " + " + two + " + " + three +" ="
	}
}

//10以内的减法填空
function fillTenMinus(){
	var total = 5+Math.round(Math.random() * 5);
	var one = Math.round(Math.random() * total);
	var second = total - one;
	
	var equalFlag = 1+Math.round(Math.random());
	//var equalFlag = 2;
	var numberFlag = 1+Math.round(Math.random());
	if(equalFlag>1){
		if(numberFlag>1){
			return {str:one + " = " + total + " - " + "____"};
		}else{
			return {str:one + " = " + "____" + " - " + second};
		}
	}else{
		if(numberFlag>1){
			return {str:total + " - " + "____" + " = " + second };
		}else{
			return {str:"____" + " - " + one + " = " + second};
		}
	}
}

//20以内的减法填空
function fillTwentyMinus(){
	var total = 11+Math.round(Math.random() * 9);
	var one = Math.round(Math.random() * total);
	var second = total - one;
	
	//var equalFlag = 1+Math.round(Math.random());
	var equalFlag = 2;
	var numberFlag = 1+Math.round(Math.random());
	if(equalFlag>1){
		if(numberFlag>1){
			return {str:one + " = " + total + " - " + "____"};
		}else{
			return {str:one + " = " + "____" + " - " + second};
		}
	}else{
		if(numberFlag>1){
			return {str:total + " - " + "____" + " = " + second };
		}else{
			return {str:"____" + " - " + one + " = " + second};
		}
	}
}

//20以内的减加填空
function innerTenPlusFill(){
	
	var firstFlag = Math.floor(Math.random() * 2);
	var secondFlag = Math.floor(Math.random() * 2);
	var fillFlag = Math.floor(Math.random()* 3);
	
	var first , second , third ,total , firstFlagStr,secondFlagStr;
	//var one = 1+Math.round(Math.random() * total-1);
	//var second =1+Math.round(Math.random() * total-1);
	
	//var numberFlag = 1+Math.round(Math.random());
	
	if(fillFlag == 0){
		//第一个数为填空
		first = "____";
		if(firstFlag == 1){
			//第一个符号为加法
			second = Math.floor(Math.random() * 10);
			firstFlagStr = " + ";
		}else{
			//第一个符号为减法
			second = Math.floor(Math.random() * 9);
			firstFlagStr = " - ";
		}
		
		if(secondFlag == 1){
			//第二个符号为加法
			third = Math.floor(Math.random() * 10);
			secondFlagStr = " + ";
			if(firstFlag == 1){
				//第一个符号为加法
				total = (second + third)+Math.floor(Math.random() * 10);
			}else{
				//第一个符号为减法
				//total = Math.floor(Math.random() * (10 - (third - second)));
				total = third+Math.round(Math.random() * 10);
			}
		}else{
			//第二个符号为减法
			third = Math.floor(Math.random() * second);
			secondFlagStr = " - ";
			if(firstFlag == 1){
				//第一个符号为加法
				//total = Math.floor(Math.random() * (10 - (third - second)));
				total = Math.abs(third+second)+Math.floor(Math.random() * 5);
			}else{
				//第一个符号为减法
				total = Math.floor(Math.random() * 5);
			}
		}
	}
	
	if(fillFlag == 1){
		//第二个数为填空
		second = "____";
		first = Math.floor(Math.random() * 10);
		if(firstFlag == 1){
			//第一个符号为加法
			firstFlagStr = " + ";
		}else{
			//第一个符号为减法
			firstFlagStr = " - ";
		}
		
		if(secondFlag == 1){
			//第二个符号为加法
			secondFlagStr = " + ";
			third = Math.floor(Math.random() * 10);
			if(firstFlag == 1){
				//第一个符号为加法
				total = (first + third)+Math.floor(Math.random() * 10);
			}else{
				//第一个符号为减法
				//total = Math.floor(Math.random() * (10 - (third - first)));
				total = Math.min(third+Math.floor(Math.random() * 5),third + first);
				
			}
		}else{
			//第二个符号为减法
			secondFlagStr = " - ";
			third = Math.floor(Math.random() * first);
			if(firstFlag == 1){
				//第一个符号为加法
				//total = Math.floor(Math.random() * (10 - (third - first)));
				total = Math.abs(third-first)+Math.floor(Math.random() * 10);
				
			}else{
				//第一个符号为减法
				total = Math.floor(Math.random() * 5);
				first = (third + total)+Math.floor(Math.random() * 10);
			}
		}
	}
	
	if(fillFlag == 2){
		//第三个数为填空
		third = "____";
		first = Math.floor(Math.random() * 10);
		if(firstFlag == 1){
			//第一个符号为加法
			firstFlagStr = " + ";
			second = Math.floor(Math.random() * 10);
		}else{
			//第一个符号为减法
			firstFlagStr = " - ";
			second = Math.floor(Math.random() * first);
		}
		
		if(secondFlag == 1){
			//第二个符号为加法
			secondFlagStr = " + ";
			if(firstFlag == 1){
				//第一个符号为加法
				total = (first + second)+Math.floor(Math.random() * 10);
			}else{
				//第一个符号为减法
				total = Math.abs(second-first)+Math.floor(Math.random() * 10);
			}
		}else{
			//第二个符号为减法
			secondFlagStr = " - ";
			if(firstFlag == 1){
				//第一个符号为加法
				total = +Math.floor(Math.random() * Math.abs(second+first));
			}else{
				//第一个符号为减法
				total = Math.floor(Math.random() * 5);
				first = (second + total)+Math.floor(Math.random() * 10);
			}
		}
	}
	
	
	return {str:first + firstFlagStr + second + secondFlagStr + third + " = " + total};
	
}

//3个数有加有减
function threePlusMinus(){
	var one = 1+Math.round(Math.random() * 8);
	var two = 1+Math.round(Math.random() * 8);
	var three = 1+Math.round(Math.random() * 8);
	
	if(one>=two){
		if((one + two - three)>0)
		{
			return {
				str:one + " + " + two + " - " + three +" ="
				}
		}else if((one - two + three)>0){
			return {
				str:one + " - " + two + " + " + three +" ="
				}
		}

	}else{
		if((one + two - three)>0){
			return {
			str:one + " + " + two + " - " + three +" ="
			}
		}

	}
}
//20以内连减
function threeMinus(){
	var two = 1+Math.round(Math.random() * 8);
	var three = 1+Math.round(Math.random() * 8);
	
	var one = 0 ;
	while(one<=(two + three)){
		one = Math.round(Math.random() * (two + three));
		var random = 1+Math.round(Math.random() * (19-one));
		one = one + random;
	}
	return {
	str:one + " - " + two + " - " + three +" ="
	}
	//return one + " - " + two + " - " + three +" =";
}

//10以内填空
function fillTen(){
	//var flag = 1+Math.round(Math.random());
	var flag = 2;
	var fillNum = 1+Math.round(Math.random());
	var one , two , flagStr , total;
	if(flag>1){
		//加法
		flagStr = " + ";
		if(fillNum>1){
			//填空在前
			one = "____";
			total = 3+Math.round(Math.random() * 7);
			two = 1+Math.round(Math.random() * (total-2));
		}else{
			//填空在后
			two = "____";
			total = 3+Math.round(Math.random() * 7);
			one = 1+Math.round(Math.random() * (total-2));
		}
	}else{
		//减法
		flagStr = " - ";
		if(fillNum>1){
			//填空在前
			one = "____";
			total = 1+Math.round(Math.random() * 4);
			two = 1+Math.round(Math.random() * (9 - total));
		}else{
			//填空在后
			two = "____";
			total = 1+Math.round(Math.random() * 6);
			one = total+Math.round(Math.random() * (9-total));
		}
	}
	
	return {
	str:one + flagStr + two +" = " + total
	}
	//return one + flagStr + two +" = " + total;
}
//20以内填空
function fillTwenty(){
	var flag = 1+Math.round(Math.random());
	//var flag = 2;
	var fillNum = 1+Math.round(Math.random());
	var one , two , flagStr , total;
	if(flag>1){
		//加法
		flagStr = " + ";
		if(fillNum>1){
			//填空在前
			one = "____";
			total = 5+Math.round(Math.random() * 14);
			two = Math.round(Math.random() * (total-1));
		}else{
			//填空在后
			two = "____";
			total = 5+Math.round(Math.random() * 14);
			one = Math.round(Math.random() * (total-1));
		}
	}else{
		//减法
		flagStr = " - ";
		if(fillNum>1){
			//填空在前
			one = "____";
			two = 1+Math.round(Math.random() * 8);
			total = 1+Math.round(Math.random() * 8);
		}else{
			//填空在后
			two = "____";
			total = 1+Math.round(Math.random() * 4);
			one = 9+Math.round(Math.random() * total);
		}
	}
	return {
	str:one + flagStr + two +" = " + total
	}
	//return one + flagStr + two +" = " + total;
}

//10以内乘法
function mutilable10(){
	var one = 1 + Math.round(Math.random() * 8);
	var two = 1 + Math.round(Math.random() * 8);
	return {
	str:one + " Ｘ " + two+" ="
	}
	//return one + " Ｘ " + two+" =";
}

//乘法填空
function mutilfill(){
	var one = 1 + Math.round(Math.random() * 8);
	var two = 1 + Math.round(Math.random() * 8);
	var total = one * two;
	
	var fillNum = 1+Math.round(Math.random());
	if(fillNum>1){
		//填空在前
		one = "____";
	}else{
		//填空在后
		two = "____";
	}
	return {
		str:one + " Ｘ " + two+" = " + total
		}
}

//10以内除法
function divable10(){
	var one = 1 + Math.round(Math.random() * 8);
	var two = 1 + Math.round(Math.random() * 8);
	var total = one * two;
	var fillNum = 1+Math.round(Math.random());
	if(fillNum>1){
		//除one
		return {
		str:total + " ÷ " + one+" = "
		}
	}else{
		//除two
		return {
		str:total + " ÷ " + one+" = "
		}
	}
}

//10以内加乘
function plusMutil10(){
	//乘法在前或在后标识
	var mutilFlag = 1+Math.round(Math.random());
	//加法或减法标识
	var plusMinusFlag = 2;
	
	var one,two,three;
	var firstOperator , secondOperator;
	if(mutilFlag>1){
		//乘法在前
		one = 1+Math.round(Math.random() * 8);
		two = 1+Math.round(Math.random() * 8);
		firstOperator = " Ｘ ";
		if(plusMinusFlag>1){
			//后面跟上加法
			secondOperator = " + ";
			three = 1+Math.round(Math.random() * 8);
		}else{
			//后面跟上减法
			secondOperator = " - ";
			three = 1+Math.round(Math.random() * (one * two));
		}
	}else{
		//乘法在后
		secondOperator = " Ｘ ";
		three = 1+Math.round(Math.random() * 8);
		two = 1+Math.round(Math.random() * 8);
		if(plusMinusFlag>1){
			//前面是加法
			firstOperator = " + ";
			one = 1+Math.round(Math.random() * 8);
		}else{
			//前面是减法
			firstOperator = " - ";
			one = (three * two)+Math.round(Math.random() * 20);
		}
	}
	return {
			str:one + firstOperator + two + secondOperator + three +" ="
			}
	;
}

//10以内加减乘
function plusMinusMutil10(){
	//乘法在前或在后标识
	var mutilFlag = 1+Math.round(Math.random());
	//加法或减法标识
	var plusMinusFlag = 1+Math.round(Math.random());
	
	var one,two,three;
	var firstOperator , secondOperator;
	if(mutilFlag>1){
		//乘法在前
		one = 1+Math.round(Math.random() * 8);
		two = 1+Math.round(Math.random() * 8);
		firstOperator = " Ｘ ";
		if(plusMinusFlag>1){
			//后面跟上加法
			secondOperator = " + ";
			three = 1+Math.round(Math.random() * 18);
		}else{
			//后面跟上减法
			secondOperator = " - ";
			three = 1+Math.round(Math.random() * (one * two));
		}
	}else{
		//乘法在后
		secondOperator = " Ｘ ";
		three = 1+Math.round(Math.random() * 8);
		two = 1+Math.round(Math.random() * 8);
		if(plusMinusFlag>1){
			//前面是加法
			firstOperator = " + ";
			one = 1+Math.round(Math.random() * 18);
		}else{
			//前面是减法
			firstOperator = " - ";
			one = (three * two)+Math.round(Math.random() * 20);
		}
	}
	return {
			str:one + firstOperator + two + secondOperator + three +" ="
			}
	;
}

//100以内加减乘
function plusMinusMutil100(){
	//乘法在前或在后标识
	var mutilFlag = 1+Math.round(Math.random());
	//加法或减法标识
	//var plusMinusFlag = 1+Math.round(Math.random());
	var plusMinusFlag = 1;
	
	var one,two,three;
	var firstOperator , secondOperator;
	if(mutilFlag>1){
		//乘法在前
		one = 1+Math.round(Math.random() * 8);
		two = 1+Math.round(Math.random() * 8);
		firstOperator = " Ｘ ";
		if(plusMinusFlag>1){
			//后面跟上加法
			secondOperator = " + ";
			three = 10+Math.round(Math.random() * 48);
		}else{
			//后面跟上减法
			secondOperator = " - ";
			three = 1+Math.floor(Math.random() * (one * two));
		}
	}else{
		//乘法在后
		secondOperator = " Ｘ ";
		three = 1+Math.round(Math.random() * 8);
		two = 1+Math.round(Math.random() * 8);
		if(plusMinusFlag>1){
			//前面是加法
			firstOperator = " + ";
			one = 10+Math.round(Math.random() * 48);
		}else{
			//前面是减法
			firstOperator = " - ";
			one = (three * two)+Math.round(Math.random() * 20);
		}
	}
	return {
			str:one + firstOperator + two + secondOperator + three +" ="
			}
	;
}

//100退位的减法
function bigLess100Minus(){
	var one,two ;
	one = 10*(1+Math.round(Math.random() * 7))+Math.round(Math.random() * 8);
	var geNum = one % 10;
	var tenNum = Math.floor(one / 10);
	two = Math.round(Math.random() * (tenNum-1))*10 + geNum + Math.round(Math.random() * (9-geNum));
	
	return {
	left:one,flag:"-",right:two,str:one + " - " + two +" = "
	}
}

//100以内的减法填空
function fill100Minus(){
	var total = 11+Math.round(Math.random() * 89);
	var one = Math.round(Math.random() * total);
	var second = total - one;
	
	var equalFlag = 1+Math.round(Math.random());
	//var equalFlag = 2;
	var numberFlag = 1+Math.round(Math.random());
	if(equalFlag>1){
		if(numberFlag>1){
			return {str:one + " = " + total + " - " + "____"};
		}else{
			return {str:one + " = " + "____" + " - " + second};
		}
	}else{
		if(numberFlag>1){
			return {str:total + " - " + "____" + " = " + second };
		}else{
			return {str:"____" + " - " + one + " = " + second};
		}
	}
}
</script>
<style>
body{overflow-x：hidden；overflow-y：auto；}

</style>

<body>
<div id="testList"></div>
</body>
</html>